package net.kem.android.positioner;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * This example taken from
 * http://www.codeproject.com/Articles/112044/positioner-
 * App-to-Find-Current-Nearest-Location-us
 *
 * @author Evgeny.Kurtser
 *
 */
public class SettingsActivity extends Activity {
	private EditText editHost;
	private EditText editPort;
	private EditText editUsername;
	private EditText editPassword;

	private Button buSave;
	private Button buCancel;

	private Button buStartService;
	private Button buStopService;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		Settings.init(this);

		editHost = (EditText) findViewById(R.id.settingsHost);
		editPort = (EditText) findViewById(R.id.settingsPort);
		editUsername = (EditText) findViewById(R.id.settingsUsername);
		editPassword = (EditText) findViewById(R.id.settingsPassword);
		buSave = (Button) findViewById(R.id.settingsButtonSave);
		buCancel = (Button) findViewById(R.id.settingsButtonCancel);

		buStartService = (Button) findViewById(R.id.settingsButtonStartService);
		buStopService = (Button) findViewById(R.id.settingsButtonStopService);

		// Restore preferences
		editHost.setText(Settings.getHost());
		editPort.setText(String.valueOf(Settings.getPort()));
		editUsername.setText(Settings.getUsername());
		editPassword.setText(Settings.getPassword());


//		Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
//		e.putString("password", mPassword);
//		e.commit();


		buSave.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				// Save user preferences. We need an Editor object to make changes.
				Settings.setHost(editHost.getText().toString());
				Settings.setPort(Integer.parseInt(editPort.getText().toString()));
				Settings.setUsername(editUsername.getText().toString());
				Settings.setPassword(editPassword.getText().toString());
				setResult(RESULT_OK);
				finish();
			}
		});
		buCancel.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				//SettingsActivity.this.setVisible(false);
				setResult(RESULT_CANCELED);
				finish();
			}
		});

		buStartService.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				startPositionerService();
				buStartService.setEnabled(false);
				buStopService.setEnabled(true);
			}
		});
		buStopService.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				stopPositionerService();
				buStartService.setEnabled(true);
				buStopService.setEnabled(false);
			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();
		boolean isPositionerServiceRunning = isPositionerServiceRunning();
		buStartService.setEnabled(!isPositionerServiceRunning);
		buStopService.setEnabled(isPositionerServiceRunning);
	}

	private boolean isPositionerServiceRunning() {
		String serviceName = PositionerService.class.getName(); // net.kem.android.positioner.PositionerService
		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
			if (serviceName.equals(service.service.getClassName())) {
				return true;
			}
		}
		return false;
	}

	private ComponentName startPositionerService() {
		Intent intent = new Intent(SettingsActivity.this, PositionerService.class);
		ComponentName res = startService(intent);
		Settings.setValue(Settings.PARAM_IS_SERVICE_RUNNING, true);
		return res;
	}
	private boolean stopPositionerService() {
		Intent intent = new Intent(SettingsActivity.this, PositionerService.class);
		boolean res = stopService(intent);
		Settings.setValue(Settings.PARAM_IS_SERVICE_RUNNING, false);
		return res;
	}
}